//
//  YXBaseViewController.swift
//  yanXiangWeiBo
//
//  Created by 颜祥 on 15/12/15.
//  Copyright © 2015年 yanxiang. All rights reserved.
//

import UIKit

class YXBaseViewController: UITableViewController,YXVistorViewDelegate
{
    
    // 用户是否已经登录
    var isUserLogin = YXUserAccount.isUserLogin
    
    // 自定义View的时候会调用这个方法
    override func loadView() {
         isUserLogin ? super.loadView() : setupVistorView()
    }
    
    
    // MARK: -设置访客视图
    func setupVistorView()
    {
        // 替换根视图
        vistorView = YXVistorView()
        view = vistorView
        
        // 指定代理
        vistorView.delegate = self
        
        // 添加导航条两边的按钮
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewRegister")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorViewLogin")
        
        
        // 选择视图
        if self is YXHomeViewController // 首页
        {
            vistorView.startRotationAnimation()
            
            // 应用进入前台，动画开始，应用退到后台，动画停止
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "appDidBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
            
            
            
        } else if self is YXMessageViewController // 消息
        {
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
            
        } else if self is YXDiscoverViewController // 发现
        {
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
            
        } else if self is YXProfileViewController // 我
        {
            vistorView.setupInfo("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }

    
    }
    
    // MARK: - 移除监听
    deinit
    {
        if self is YXHomeViewController
        {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    
    
    // MARK: -YXVistorViewDelegate代理方法
    // 点击注册
    func vistorViewRegister()
    {
        //print(__FUNCTION__)
    }
    
    // 点击登录
    func vistorViewLogin()
    {
        // 包装导航控制器
        let oauth = YXOauthViewController()
        
        let nav = UINavigationController(rootViewController: oauth)
        
        presentViewController(nav, animated: true, completion: nil)

    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    
    // MARK: -监听应用程序的状态
    // 应用退到后台
     func appDidEnterBackground()
    {
        vistorView.pauseAnimation()
    }
    
    // 应用进入到前台
    func appDidBecomeActive()
    {
        vistorView.resumAnimation()
    }
    
    // MARK: - 懒加载访客视图
    lazy var vistorView: YXVistorView = YXVistorView()
    

}
